package com.android.jtpay.u3ddemo;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.unity3d.player.UnityPlayerActivity;
import com.wap.pay.activity.PayWebViewActivity;
import com.wap.pay.interfaces.NetworkCallback;
import com.wap.pay.model.WapPayInfo;
import com.wap.pay.view.PayWebView;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends UnityPlayerActivity {
    private static final String PAY_INFO = "获取支付信息...";
    private Toast mToast;
    String[][] OPTION_PARAM = {{"p8_signtype", ""}, {"p10_paychannelnum", ""}, {"p11_cardtype", ""},
            {"p12_channel", ""}, {"p13_orderfailertime", ""}, {"p15_customcontacttype", ""},
            {"p16_customcontact", ""}, {"p18_product", ""}, {"p19_productcat", ""},
            {"p20_productnum", ""}, {"p21_pdesc", ""}, {"p22_version", ""},
            {"p23_charset", ""}, {"p24_remark", ""}};
    private ProgressDialog mProgressDialog;

    private PayWebView mPayWebView;

    /**
     * simulate user ,get url
     * the url url for user, and then call our sdk to pay
     *
     * @param code       merchant code
     * @param p7_sign    Merchant transfer parameter signature
     * @param money      pay money
     * @param returnUrl  User jump address for display payment results
     * @param noticeUrl  Notice of payment
     * @param baseUrl    User BASE_URL
     * @param payType    User pay type
     * @param order      User order
     * @param orderTime  User Order Adding Time
     * @param customName Merchant Name
     * @param customIp   Merchant IP
     * @param terminal   Terminal equipment fixed value
     * @param iswappay   Pay environment fixed value
     * @return wapPayInfo
     */
    private WapPayInfo getWebPayInfo(String money, String code, String p7_sign, String returnUrl,
                                     String noticeUrl, String baseUrl, String payType,
                                     String order, String orderTime, String customName,
                                     String customIp, String terminal, String iswappay) {
        WapPayInfo wapPayInfo = new WapPayInfo();
        wapPayInfo.setMoney(money);
        wapPayInfo.setCode(code);
        wapPayInfo.setP7_sign(p7_sign);
        wapPayInfo.setReturnUrl(returnUrl);
        wapPayInfo.setNoticeUrl(noticeUrl);
        wapPayInfo.setBaseUrl(baseUrl);
        wapPayInfo.setPayType(payType);
        wapPayInfo.setOrderTime(orderTime);
        wapPayInfo.setOrder(order);
        wapPayInfo.setCustomName(customName);
        wapPayInfo.setCustomIp(customIp);
        wapPayInfo.setTerminal(terminal);
        wapPayInfo.setIswappay(iswappay);
        /**Following options*/
        Map<String, String> optionalMap = new HashMap<>();
        for (String[] param : OPTION_PARAM) {
            if (!TextUtils.isEmpty(param[1])) {
                optionalMap.put(param[0], param[1]);
            }
        }
        wapPayInfo.setOptionalMap(optionalMap);
        return wapPayInfo;
    }
    /**
     * simulate user ,get url
     * the url url for user, and then call our sdk to pay
     *
     * @param code       merchant code
     * @param p7_sign    Merchant transfer parameter signature
     * @param money      pay money
     * @param returnUrl  User jump address for display payment results
     * @param noticeUrl  Notice of payment
     * @param baseUrl    User BASE_URL
     * @param payType    User pay type
     * @param order      User order
     * @param orderTime  User Order Adding Time
     * @param customName Merchant Name
     * @param customIp   Merchant IP
     * @param terminal   Terminal equipment fixed value
     * @param iswappay   Pay environment fixed value
     * @param optionalMap   Optional parameters
     * @return WapPayInfo
     */
    private WapPayInfo getWebPayInfo(String money, String code, String p7_sign, String returnUrl,
                                     String noticeUrl, String baseUrl, String payType,
                                     String order, String orderTime, String customName,
                                     String customIp, String terminal, String iswappay,
                                     Map<String, String> optionalMap) {
        WapPayInfo wapPayInfo = new WapPayInfo();
        wapPayInfo.setMoney(money);
        wapPayInfo.setCode(code);
        wapPayInfo.setP7_sign(p7_sign);
        wapPayInfo.setReturnUrl(returnUrl);
        wapPayInfo.setNoticeUrl(noticeUrl);
        wapPayInfo.setBaseUrl(baseUrl);
        wapPayInfo.setPayType(payType);
        wapPayInfo.setOrderTime(orderTime);
        wapPayInfo.setOrder(order);
        wapPayInfo.setCustomName(customName);
        wapPayInfo.setCustomIp(customIp);
        wapPayInfo.setTerminal(terminal);
        wapPayInfo.setIswappay(iswappay);

        for (Map.Entry param : optionalMap.entrySet()) {
            if (TextUtils.isEmpty((String) param.getValue())) {
                optionalMap.remove(param);
            }
        }
        wapPayInfo.setOptionalMap(optionalMap);
        return wapPayInfo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPayWebView = new PayWebView(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        dismissProgressDialog();
    }


    private void showProgressDialog() {
        if (null == mProgressDialog) {
            mProgressDialog = ProgressDialog.show(MainActivity.this, "", "", false, true, null);
        }
        mProgressDialog.setMessage(PAY_INFO);
        mProgressDialog.show();
    }

    private void dismissProgressDialog() {
        if (null != mProgressDialog) {
            mProgressDialog.dismiss();
        }
    }
    public void showToast(final String text) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {

            public void run() {
                if (mToast == null) {
                    mToast = Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT);
                } else {
                    mToast.setText(text);
                }
                mToast.show();
            }
        });
    }
    /**No optional parameters*/
    public void wappayActivity(final String money, final String code,final  String p7_sign, final  String returnUrl,
                               final String noticeUrl, final String baseUrl,final  String payType,
                               final String order, final String orderTime, final String customName,
                               final String customIp,final  String terminal, final String iswappay){
        WapPayInfo wapPayInfo = getWebPayInfo(
                money, code, p7_sign, returnUrl,noticeUrl, baseUrl, payType,
                order, orderTime, customName,customIp, terminal, iswappay);
        Log.d("junfutong", "wapPayInfo = " + wapPayInfo.toString());
        PayWebViewActivity.startActivity(wapPayInfo, MainActivity.this);
    }
    /**With optional parameters*/
    public void wappayActivity(final String money, final String code,final  String p7_sign, final  String returnUrl,
                               final String noticeUrl, final String baseUrl,final  String payType,
                               final String order, final String orderTime, final String customName,
                               final String customIp,final  String terminal, final String iswappay,
                               final String optionalString){
        Map<String,String> map=StringToHashmap.toHashMap(optionalString);
        WapPayInfo wapPayInfo = getWebPayInfo(
                money, code, p7_sign, returnUrl,noticeUrl, baseUrl, payType,
                order, orderTime, customName,customIp, terminal, iswappay,map);
        Log.d("junfutong", "wapPayInfo = " + wapPayInfo.toString());
        PayWebViewActivity.startActivity(wapPayInfo, MainActivity.this);
    }
    /**With optional parameters*/
    public void wappayWebview(final String money, final String code,final  String p7_sign, final  String returnUrl,
                              final String noticeUrl, final String baseUrl,final  String payType,
                              final String order, final String orderTime, final String customName,
                              final String customIp,final  String terminal, final String iswappay,
                              final String optionalString){
        Map<String,String> map=StringToHashmap.toHashMap(optionalString);
        WapPayInfo wapPayInfo = getWebPayInfo(
                money, code, p7_sign, returnUrl,noticeUrl, baseUrl, payType,
                order,orderTime, customName,customIp, terminal, iswappay,map);
        Log.d("junfutong", "wapPayInfo = " + wapPayInfo.toString());
        mPayWebView.loadUrl(new NetworkCallback() {
            @Override
            public void onResult(boolean success) {
                if (success) {
                    dismissProgressDialog();
                } else {
                    showProgressDialog();
                }
            }
        }, wapPayInfo);
    }
    /**No optional parameters*/
    public void wappayWebview(final String money, final String code,final  String p7_sign, final  String returnUrl,
                              final String noticeUrl, final String baseUrl,final  String payType,
                              final String order, final String orderTime, final String customName,
                              final String customIp,final  String terminal, final String iswappay){
        WapPayInfo wapPayInfo = getWebPayInfo(
                money, code, p7_sign, returnUrl,noticeUrl, baseUrl, payType,
                order,orderTime, customName,customIp, terminal, iswappay);
        Log.d("junfutong", "wapPayInfo = " + wapPayInfo.toString());
        mPayWebView.loadUrl(new NetworkCallback() {
            @Override
            public void onResult(boolean success) {
                if (success) {
                    dismissProgressDialog();
                } else {
                    showProgressDialog();
                }
            }
        }, wapPayInfo);
    }


}
